namespace taurus.client{
    /**
	 * 日志等级
	 */
	enum LoggerLevel{
		DEBUG,
		INFO,
		WARN,
		ERROR,
		TRACE
    }
    
    function stringFormat(str:string,...args:any[]):string {
        var result = str;
        if (args.length >0) {    
            for (var i = 0; i < args.length; i++) {
                if (args[i] != undefined) {
                    var reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, args[i]);
                }
            }
        }
        return result;
    }

    

    /**
     * Logger日志类
     * @author daixiwei	daixiwei15@126.com
     */
    export class Logger{
        protected _print(level:LoggerLevel,str:string){
            var date = new Date();
            var msg = stringFormat("[{0}:{1}:{2}]-> {3}", date.getHours(), date.getMinutes(), date.getMilliseconds(), str);
            if(level == LoggerLevel.WARN){
                console.warn(msg);
            }else if(level == LoggerLevel.ERROR){
                console.error(msg);
            }else{
                console.info(msg);
            }
        }
        

    
        public debug(msg:any, ...params:any[]) {
            var str = msg.toString();
            if(params && params.length>0){
                str = stringFormat(msg.toString(), ...params);
            }
            this._print(LoggerLevel.DEBUG,str);
        }

        public info(msg:any, ...params:any[]) {
            var str = msg.toString();
            if(params && params.length>0){
                str = stringFormat(str, ...params);
            }
            this._print(LoggerLevel.INFO,str);
            
        }

        public warn(msg:any, ...params:any[]) {
            var str = msg.toString();
            if(params && params.length>0){
                str = stringFormat(str, ...params);
            }
            this._print(LoggerLevel.WARN, str);
        }
    
        public error(msg:any, ...params:any[]) {
            var str = msg.toString();
            if(params && params.length>0){
                str = stringFormat(str, ...params);
            }
            this._print(LoggerLevel.ERROR,str);
        }
    }

    export let log:Logger = new Logger();
}
